/**
 * This is a tutorial source code 
 * provided "as is" and without warranties.
 *
 * For any question please visit the web site
 * http://www.survivingwithandroid.com
 *
 * or write an email to
 * survivingwithandroid@gmail.com
 *
 */
package com.tecnoagrosoft.zafra.control.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;



import android.util.Log;

/*
 * Copyright (C) 2013 Surviving with Android (http://www.survivingwithandroid.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
public class WeatherHttpClient {

	private static String BASE_URL = "http://api.openweathermap.org/data/2.5/weather?q=";
	private static String FORMAT_URL = "&lang=sp&units=metric";

//	private static String BASE_FORECAST_URL = "http://api.openweathermap.org/data/2.5/forecast/daily?mode=json&q=";
	

	public String getWeatherData(String location) {
		HttpURLConnection con = null;
		InputStream is = null;

		try {

			URL url = new URL(BASE_URL + location + FORMAT_URL);

			con = (HttpURLConnection) url.openConnection();
			con.setReadTimeout(110000);
			con.setConnectTimeout(15000);
			con.setRequestMethod("GET");
			con.setDoInput(true);
			con.setDoOutput(true);
			con.connect();

			// Let's read the response
			
			is = con.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
			StringBuffer buffer = new StringBuffer();
			String line = null;
			while ((line = br.readLine()) != null){
				buffer.append(line + "\n");
			
			}			
			is.close();
			con.disconnect();
			return buffer.toString();

		}
		catch (MalformedURLException ex) {
			Log.e("getWeatherData MalformedURLException", ex.toString());
		} catch (IOException ex) {
			Log.e("getWeatherData IOException", ex.toString());
			
		}

		finally {
			try {
				is.close();
			} catch (Throwable t) {
			}
			try {
				con.disconnect();
			} catch (Throwable t) {
			}
		}
		return null;

	}

//	public String getForecastWeatherData(String location, String lang,
//			String sForecastDayNum) {
//		HttpURLConnection con = null;
//		InputStream is = null;
//		int forecastDayNum = Integer.parseInt(sForecastDayNum);
//
//		try {
//
//			// Forecast
//			String url = BASE_FORECAST_URL + location;
//			
//			if (lang != null)
//				url = url + "&lang=" + lang;
//
//			url = url + "&cnt=" + forecastDayNum;
//			con = (HttpURLConnection) (new URL(url)).openConnection();
//			con.setRequestMethod("GET");
//			con.setReadTimeout(20000);
//			con.setConnectTimeout(25000);
//			con.setDoInput(true);
//			con.setDoOutput(true);
//			con.connect();
//
//			// Let's read the response
//			StringBuffer buffer1 = new StringBuffer();
//			is = con.getInputStream();
//			BufferedReader br1 = new BufferedReader(new InputStreamReader(is));
//			String line1 = null;
//			while ((line1 = br1.readLine()) != null)
//				buffer1.append(line1 + "\r\n");
//
//			is.close();
//			con.disconnect();
//			return buffer1.toString();
//		}
//		catch (MalformedURLException ex) {
//			Log.e("Error en getForecastWeatherData MalformedURLException", ex.toString());
//		} catch (IOException ex) {
//			Log.e("Error en getForecastWeatherData IOException", ex.toString());
//		} finally {
//			try {
//				is.close();
//			} catch (Throwable t) {
//			}
//			try {
//				con.disconnect();
//			} catch (Throwable t) {
//			}
//		}
//
//		return null;
//
//	}


}
